package com.org.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.org.common.PageBean;
import com.org.common.Result;
import com.org.pojo.Collect;
import com.org.pojo.Serve;
import com.org.service.ICollectService;
import com.org.service.ServeService;
import com.org.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 收藏表 前端控制器
 * </p>
 *
 * @author cjx
 * @since 2024-02-27
 */
@RestController
@RequestMapping("/collect")
public class CollectController {

    @Autowired
    private ICollectService collectService;

    // 添加收藏
    @PostMapping("/add")
    public Result addNewCollect(@RequestBody Collect collect) {
        collect.setCreateTime(LocalDateTime.now());
        return collectService.save(collect)?Result.success(collect):Result.error("添加失败");
    }

    // 查看收藏详情
    @GetMapping("/get")
    public Result<Collect> getCollectDetail(long collectId){
        return Result.success(collectService.getById(collectId));
    }

    // 分页查询收藏
    // 因为一般都是用户查看自己的收藏，而且管理员端没必要做收藏管理，所以 userId 必传
    @GetMapping("/list")
    public Result<PageBean<Collect>> getCollects(
            Integer pageNum,    //第几页
            Integer pageSize,   //页面容量
            String userId,     //用户id 不可为空
            Boolean isUp,        //true：按时间升序排序，false：降序 不可为空
            String serveId      //服务id 可为空，可不传
    ){

        Page<Collect> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);

        LambdaQueryWrapper<Collect> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Collect::getUserId, userId);
        if (serveId != null && !serveId.isEmpty()){
            lambdaQueryWrapper.eq(Collect::getServeId, serveId);
        }
        // isUp 为真，则按时间升序
        lambdaQueryWrapper.orderBy(isUp, true, Collect::getCreateTime);
        // 否则按时间降序
        lambdaQueryWrapper.orderBy(!isUp, false, Collect::getCreateTime);

        IPage<Collect> iPage = collectService.page(page, lambdaQueryWrapper);

        PageBean<Collect> pageBean = new PageBean<>();
        pageBean.setTotal(iPage.getTotal());
        pageBean.setItems(iPage.getRecords());

        System.out.println(pageBean);

        return Result.success(pageBean);
    }

    // 删除收藏
    @GetMapping("/delete")
    public Result deleteCollect(long collectId){
        return collectService.removeById(collectId)?Result.success():Result.error("删除收藏失败");
    }

    // 查看自己收藏的数量
    @GetMapping("/count")
    public Result count(){
        Map<String, Object> param = ThreadLocalUtil.get();
        Integer userId = (Integer) param.get("id");
        Long count = collectService.count(new LambdaQueryWrapper<Collect>().eq(Collect::getUserId, userId));
        return Result.success(count);
    }
}
